<template>
  <div >
    <div id="ww_login">
    </div>
  </div>

</template>

<script>
import {wechatUserIdLogin} from "@/api/login";
import {setToken} from '@/utils/auth'
export default {
  name: "Index",
  data() {
    return {
      // 版本号
      version: "3.8.8"
    };
  },
  mounted() {
    // 初始化登录组件
    this.weLogin();
    // console.log(this.$ww, 'ww');
    // console.log(process.env.VUE_APP_APPID, 'VUE_APP_APPID');
    // console.log(process.env.VUE_APP_BACKURL, 'BACKURL');
  },
  methods: {
    goTarget(href) {
      window.open(href, "_blank");
    },
    weLogin() {
     // this.$refs.weChatCodeLogin.appendChild(wwLogin.el);
     const self = this;
      this.$ww.createWWLoginPanel({
        el: '#ww_login',
        params: {
          login_type: 'ServiceApp',
         // agentid: '1000157',
          appid: process.env.VUE_APP_APPID,
          redirect_uri: process.env.VUE_APP_BACKURL,
          state: 'loginState',
          redirect_type: 'callback',
        },
        onCheckWeComLogin({ isWeComLogin }) {
          console.log(isWeComLogin, 'onCheckWeComLogin')
        },
        onLoginSuccess({ code }) {
          console.log({ code }, 'onLoginSuccess')
          self.codeToToken(code)
        },
        onLoginFail(err) {
          console.log(err, 'fail')
        },
      })
    },
    async codeToToken(code){
     // console.log('进入请求');
      let res = await wechatUserIdLogin({ "code": code })
      if(res.code == 200){
        //this.$router.push({path:'/home'})
       this.$store.commit('SET_TOKEN', res.token)
       setToken(res.token)
      }
      console.log(res,'wechatUserIdLogin');
    }
  }
};
</script>

<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }

  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }

  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
  }

  font-family: "open sans",
  "Helvetica Neue",
  Helvetica,
  Arial,
  sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;

  ul {
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>
